import { MenuItemDefinition } from '@amcax/base';
import { getKeyConstraintArr } from '@app-base/environments/product/utils';
import { ctx2 } from '@app-cad/module';
import { HookAPI } from 'antd/es/modal/useModal';
import { isEmpty } from 'lodash';
import { RefDocTypes } from '../../../../../../pages/product-support/consts';
import { ProductTreeNode } from '../../types';

export const removeAssemblyChild: MenuItemDefinition<ProductTreeNode> = {
  id: 'remove-assembly-child',
  children: '移除',
  hidden: ({ data, props: { parent, type } }) =>
    parent !== data.docTree || !RefDocTypes.includes(type),
  onClick: async ({ data, props: doc }) => {
    const { title, key } = doc;

    const constraintArr = getKeyConstraintArr(data?.docTree, key);

    const getContent = () => {
      // 删除零件时会把所有的有关约束删除,所以这边只需要显示
      const constraintNameArr = constraintArr?.map(({ title }) => title);
      if (!isEmpty(constraintArr)) {
        return `确认移除 ${title} 吗？同时会移除零件的约束 ${constraintNameArr?.join(',')}。`;
      }
      return `确认移除 ${title} 吗？`;
    };

    const confirmed = await (data.modal as HookAPI).confirm({
      closable: true,
      title: '移除文档',
      content: getContent(),
      okText: '移除',
      okType: 'danger',
      cancelText: '取消',
    });

    if (confirmed) {
      await ctx2.commandCenters.basicCommands.deleteObjects([key]);
    }
  },
};
